package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.ISignService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("signs")
public class SignController {
    @Autowired
    private ISignService signService;


    //签到功能 : 保存一条签到数据
    @PostMapping
    public JSONResult saveSign() {

        JSONResult json = null;
        try {
            User user = UserContext.getCurrentUser();
            json = new JSONResult();
            Sign record = new Sign();
            record.setUser(user);
            record = signService.save(record);
            json.setResult(record);
            System.out.println(record);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    //查询功能 : 根据用户id 查询用户积分表信息
    @GetMapping
    public JSONResult selectSign() {
        JSONResult json = new JSONResult();
        User user = UserContext.getCurrentUser();
        Sign record = new Sign();
        record.setUser(user);
        record = signService.selectOne(record);
        json.setResult(record);
        return json;
    }

    //带商品价值积分过来扣除我的积分
    @GetMapping("/productScore")
    public void mySign(int productScore,Long productId){
        User user = UserContext.getCurrentUser();
        signService.setMySign(productScore,productId,user.getId());
    }

    //查询商品兑换时间
    @GetMapping("/changeTime")
    public Date changeTime(){
        return signService.selectChangeTime();
    }


    //根据用户id去查用户签到总天数
    @GetMapping("/userId")
    public JSONResult selectCount() {
        JSONResult json = new JSONResult();
        User user = UserContext.getCurrentUser();
        int allCount = signService.selectCount(user.getId());
        json.setResult(allCount);
        return json;
    }


    //查询昨天的续签天数yesterdayCount
    @GetMapping("/yesterdayCount")
    public JSONResult yesterdayCount() {
        JSONResult json = new JSONResult();
        Integer yesterdayCount;
        User user = UserContext.getCurrentUser();
        //判断昨天有没有签到(没签则空指针)
        int SignCount = signService.selectBySignTime(user.getId());
        if (SignCount >0) {
             yesterdayCount = signService.yesterdayCount(user.getId());
            json.setResult(yesterdayCount);
        }else {
            yesterdayCount =0;
            json.setResult(yesterdayCount);
        }
        return json;
    }

    //查询用户今天有没有签到getSignByToday
    @GetMapping("/getSignByToday")
    public JSONResult getSignByToday() {
        JSONResult json = new JSONResult();
        User user = UserContext.getCurrentUser();
        Sign record = new Sign();
        record.setUser(user);
        record = signService.getSignByToday(record);
        json.setResult(record);
         return json;
    }
}
